package de.tabuma.wsdl2xform;

import java.io.*;
import java.util.logging.*;
import javax.servlet.*;
import javax.servlet.http.*;
import com.google.appengine.api.users.*;

import de.tabuma.wsdl2xform.xform.logik.*;
import de.tabuma.wsdl2xform.xform.entity.Project;

import de.tabuma.soap.entity.wsdl.TDefinitions;
import de.tabuma.soap.logik.WsdlFacade;

/**
 * @author $Author: alefen $
 * @version $Revision: 1.1 $
 * @since $Date: 2009/12/30 01:10:56 $
*/
public class wsdlfile extends HttpServlet {

    private static final Logger log = Logger.getLogger(wsdlfile.class.getName());

    /** 
     * Processes requests for both HTTP <code>GET</code> and <code>POST</code> methods.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    protected void processRequest(HttpServletRequest request, HttpServletResponse response) throws ServletException, IOException {
        response.setContentType("text/xml;charset=UTF-8");
        PrintWriter out = response.getWriter();
        try {

            UserService userservice = UserServiceFactory.getUserService();
            User user = userservice.getCurrentUser();
            
            int projectID = 0;
            if(request.getParameter(de.tabuma.wsdl2xform.xform.constants.PROJECT_ID)!=null) {
                projectID = Integer.valueOf(request.getParameter(de.tabuma.wsdl2xform.xform.constants.PROJECT_ID));
            }
            log.fine("ProjectID:"+projectID);
            if(projectID!=0) {
                Project project = ProjectFacade.getProject(projectID);
                log.fine("Project:"+project);
                if(project!=null) {

                    if(project.getUser().equals(user.getEmail())||userservice.isUserAdmin()) {
                        Long id = project.getWsdlFile();
                        TDefinitions wsdlFile = de.tabuma.soap.logik.PMF.get(id,new TDefinitions());
                        log.fine("TDefinitions:"+wsdlFile);
                        String str = WsdlFacade.getWsdlDocument(wsdlFile);
                        out.println(str);
                    }
                    else {
                        out.println("You are not allowed to log into project with <i>" + de.tabuma.wsdl2xform.xform.constants.PROJECT_ID + "="+projectID+"</i>.");
                    }
                }
                else {
                    out.println("No project found for parameter <i>" + de.tabuma.wsdl2xform.xform.constants.PROJECT_ID + "="+projectID+"</i> .");
                }
            }
            else {
                out.println("No parameter <i>" + de.tabuma.wsdl2xform.xform.constants.PROJECT_ID + "</i> found.");
            }
        }
        catch(Exception e) {
            e.printStackTrace(out);
        }
        finally {
            out.close();
        }
    }
    // <editor-fold defaultstate="collapsed" desc="HttpServlet methods. Click on the + sign on the left to edit the code.">
    /** 
     * Handles the HTTP <code>GET</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doGet(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    } 

    /** 
     * Handles the HTTP <code>POST</code> method.
     * @param request servlet request
     * @param response servlet response
     * @throws ServletException if a servlet-specific error occurs
     * @throws IOException if an I/O error occurs
     */
    @Override
    protected void doPost(HttpServletRequest request, HttpServletResponse response)
    throws ServletException, IOException {
        processRequest(request, response);
    }

    /** 
     * Returns a short description of the servlet.
     * @return a String containing servlet description
     */
    @Override
    public String getServletInfo() {
        return "Short description";
    }// </editor-fold>
}